package pc.system;

import javax.servlet.*;
import pc.util.*;

public class Request
{

  private ServletRequest request = null;
  public Request()
  {
  }

  public void setRequest(ServletRequest request)
  {
    this.request = request;
  }

  public String getValue(String name)
  {
    if (request != null) return request.getParameter(name);
    
    return null;
  }

  public String getValueString(String name)
  {
    String value = getValue(name);
    return (value == null) ? "" : value;
  }

  public java.sql.Date getValueDate(String name,java.sql.Date def)
  {
    String s = getValue(name);
    if (s == null) return def;
    if (s.equals("")) return def;
    if (s.equals(Utils.nullDate.toString())) return Utils.nullDate;
    if (Utils.isDateStr(s)) return Utils.strToDate(s);
    try {
      return java.sql.Date.valueOf(s);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
      return def;
    }
  }

  public java.sql.Date getValueDate(String name)
  {
    return getValueDate(name, Utils.nullDate);
  }

  public java.sql.Time getValueTime(String name)
  {
    String s = getValue(name);
    if (s == null) return Utils.nullTime;
    if (s.equals("")) return Utils.nullTime;
    if (s.equals(Utils.nullTime.toString())) return Utils.nullTime;
    return Utils.strToTime(s);
  }


  public String getStringValue(String name, String def)
  {
    String value = getValue(name);
    if (value == null) value = def;
    return value;
  }

  public String getStringValue(String name)
  {
    return getStringValue(name,"");
  }

  public int getIntValue(String name, int def)
  {
    String value = getValue(name);
    return value == null ? def : Utils.strToInt(value);
  }

  public int getIntValue(String name)
  {
    return getIntValue(name,0);
  }

  public long getLongValue(String name, long def)
  {
    String value = getValue(name);
    return value == null ? def : Utils.strToLong(value);
  }

  public long getLongValue(String name)
  {
    return getLongValue(name,0);
  }

  public float getFloatValue(String name, float def)
  {
    String value = getValue(name);
    return value == null ? def : Utils.strToFloat(value);
  }


  public float getFloatValue(String name)
  {
    return getFloatValue(name,0);
  }

  public double getDoubleValue(String name, double def)
  {
    String value = getValue(name);
    return value == null ? def : Utils.strToDouble(value);
  }

  public double getDoubleValue(String name)
  {
    return getDoubleValue(name,0);
  }

  public String getDateValue(String name)
  {
    return Utils.toString(Utils.strToDate(getValue(name)));
  }

  public String getTimeValue(String name)
  {
    return this.getValue(name);
  }

}
